import { message } from '@/utils/resetMessage'
import { ukeyCheck } from '@/api/login'
import Middle from '@/utils/middle.js'

var ctrl = null
var websock = true
var ArmHandle = null // 加密锁句柄
var b = new Base64()
var Index = 0

export function OnLoad () {
  return new Promise((resolve, reject) => {
    try {
      ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
      resolve(ctrl)
    } catch (e) {
      ctrl = null
      websock = false
      message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
      Middle.$emit('logout')
      resolve()
    }
  })
}

// 查找RockeyArm
export function Arm_Enum () {
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    try {
      ctrl.Arm_Enum()
    } catch (e) {
      message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
      Middle.$emit('logout')
    }
  } else {
    try {
      ctrl.Arm_Enum(function (result, response) {
        if (!result) {
          // alert("Arm_Enum error. " + response);
          message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
          Middle.$emit('logout')
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        } else {
          // var rtn = response
          Arm_Open()
        }
      })
    } catch (e) {
      ctrl = null
      websock = false
      message.error('ukey验证失败')
      Middle.$emit('logout')
    }
  }
}

// 打开RockeyArm
export function Arm_Open () {
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    ArmHandle = ctrl.Arm_Open(parseInt(Index))
  } else {
    ctrl.Arm_Open(function (result, response) {
      if (!result) {
        // alert("Arm_Open error. " + response);
        message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
        Middle.$emit('logout')
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        ArmHandle = response
        Arm_VerifyPIN()
      }
    }, Index)
  }
}
// 验证PIN码
export function Arm_VerifyPIN () {
  var Handle = ArmHandle
  var UserPin = 'likonghuakang123'
  var UserType = 1
  // 获取要验证的用户类型
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_VerifyPIN(parseInt(Handle), UserType, UserPin)
    if (rtn === '0') {
      // message.error('验证PIN码成功')
      // alert("<spring:message code='验证PIN码成功'></spring:message>")
    } else {
      alert(rtn)
    }
  } else {
    ctrl.Arm_VerifyPIN(function (result, response) {
      if (!result) {
        // alert("Arm_VerifyPIN error. " + response);
        message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
        Middle.$emit('logout')
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        if (rtn === '0') {
          Arm_ReadFile()
        } else {
          // alert(rtn);
          message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
          Middle.$emit('logout')
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        }
      }
    }, parseInt(Handle), UserPin, UserType)
  }
}

// 读文件
export function Arm_ReadFile () {
  var Handle = ArmHandle
  var FileID = 1
  var ReadLength = 1024
  var FileOffset = 0
  var FileType = 1

  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_ReadFile(parseInt(Handle), parseInt(FileID), parseInt(FileOffset), parseInt(ReadLength))
  } else {
    ctrl.Arm_ReadFile(function (result, response) {
      if (!result) {
        // alert("Arm_ReadFile error. " + response);
        message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
        Middle.$emit('logout')
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        var DataInput = b.decode(rtn.split('AAAAAAAAAA')[0].replace(/\s+/g, '')).match(/[A-Za-z0-9+/=]+/g)[0]
        var form = {
          ukey: DataInput
        }
        ukeyCheck(form).then(res => {
          Arm_Close()
        }).catch(e => {
          message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
          Middle.$emit('logout')
          Arm_Close()
        })
      }
    }, parseInt(Handle), FileID, FileOffset, ReadLength)
  }
}

// 关闭RockeyArm
export function Arm_Close () {
  var Handle = ArmHandle
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_Close(parseInt(Handle))
    if (rtn === '0') {
      alert("<spring:message code='关闭锁成功'></spring:message>")
    } else {
      // alert(rtn);
      message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
      Middle.$emit('logout')
      // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
    }
  } else {
    ctrl.Arm_Close(function (result, response) {
      if (!result) {
        // alert("Arm_Close error. " + response);
        message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
        Middle.$emit('logout')
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        if (rtn !== '0') {
          // alert(rtn);
          message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
          Middle.$emit('logout')
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        }
      }
    }, Handle)
  }
}

export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function (Vue) {
    Vue.prototype.OnLoadCheck = (param) => OnLoad(param)
    Vue.prototype.Arm_Enum_Check = (param) => Arm_Enum(param)
  }
}
